#include "FoodSource.h"
#include "AntWorld.h"
#include "TextureManager.h"
#include "Game.h"
#include "GLCuboid.h"
#include <GL\glut.h>

FoodSource::FoodSource(const Vec3f& pos, const std::string& texPath, int id) : m_depleted(false),
	m_decayTime(0), m_nourishmentRemaining(0), m_contactRadius(2.0f), m_cuboid(.5,3,.5)
{
	m_id = id;
	m_pos = pos;
	m_texPath = texPath;
	m_contactRadiusSqr = m_contactRadius* m_contactRadius;

	m_nourishmentRemaining = 6;
	CreateLeaves();

}

void FoodSource::RemoveNourishment()
{
	m_nourishmentRemaining--;
	if(m_nourishmentRemaining <= 0)
	{
		m_depleted = true;
	}

}

void FoodSource::CreateLeaves()
{
	int i=0;
	int NumL = m_nourishmentRemaining;
	float x=-1,  y=1,  z=-1;
	Vec3f TheCord;

	while (i<m_nourishmentRemaining)
	{
		while (x<=1)
		{
			TheCord.X=x,TheCord.Y=y,TheCord.Z=z;
			m_leafVec.push_back(TheCord);
			x++;
			i++;
			if(i>m_nourishmentRemaining)
			{
				return;
			}
		}

		x=-1;
		z=1;
		while (x<=1)
		{
			TheCord.X=x,TheCord.Y=y,TheCord.Z=z;
			m_leafVec.push_back(TheCord);
			x++;
			i++;		
			if(i>m_nourishmentRemaining)
			{
				return;
			}
		}

		if(i>m_nourishmentRemaining)
		{
			TheCord.X=-1,TheCord.Y=y,TheCord.Z=0;
			m_leafVec.push_back(TheCord);
		}
		i++;

		if(i>m_nourishmentRemaining)
		{
			TheCord.X=1,TheCord.Y=y,TheCord.Z=0;
			m_leafVec.push_back(TheCord);
		}
		i++;

	}
}

void FoodSource::Draw()
{
	/*
	glPushMatrix();

	glTranslatef(m_pos.X, m_pos.Y, m_pos.Z);

	

	glutSolidTeapot(1.0f);


	glPopMatrix();
	*/


	glPushMatrix();
	
		//glDisable(GL_LIGHTING);
		
	
		TheTextureManager::Instance()->DrawTexture(m_texPath);

		glTranslatef(m_pos.X, m_pos.Y, m_pos.Z);	
		//GLCuboid TheCube(.5,3,.5);


		m_cuboid.Draw();

		
		glDisable(GL_TEXTURE_2D);

		//glColor3f(.1,1,.3);
		glColor3f(1,0,0);
		int i=0;
		unsigned int test = m_leafVec.size();

		while (i< m_nourishmentRemaining)
		{
			Vec3f Co;
			Co = m_leafVec[i];
			glPushMatrix();
			glTranslatef(Co.X,Co.Y,Co.Z);
			glutSolidSphere(.3,10,10);
			glPopMatrix();
			i++;
		}
		glEnable(GL_TEXTURE_2D);

	glPopMatrix();
}

void FoodSource::Update()
{

	if(m_depleted)
	{
		m_decayTime++;
		if(m_decayTime > 25)
		{
			TheAntWorld::Instance()->CreateNewFood();
			TheAntWorld::Instance()->ClearFood(m_id);
			TheGame::Instance()->DeleteGameObject(m_id);
		}
	}
}

float FoodSource::GetContactRadius()
{
	return m_contactRadius;
}
float FoodSource::GetContactRadiusSqr()
{
	return m_contactRadiusSqr;

}

bool FoodSource::IsDepleted() const
{
	return m_depleted;
}